import {
  addOrder,
  getOrderList
} from '../../api/orderApi.js'

import router from '../../router/index.js'

export default {
  namespaced: true,
  state: {
    // 配送费
    expressfee: 0.00,
    // 优惠金额
    discountamount: 0.00,
    // 当前登录客户的订单列表
    order_list: [],
    // 订单状态映射
    order_status: {
      '0': '客户已下单',
      '2': '客户已付款',
      '4': '商家已配货',
      '6': '商家已发货',
      '8': '客户已收货',
      '10': '客户已评价',
      '30': '客户申请退款',
      '32': '商家已退款',
      '34': '商家退款失败',
      '40': '订单已取消',
    }
  },
  getters: {},
  mutations: {
    set_order_list(state, data) {
      state.order_list = data;
    }
  },
  actions: {
    //当前登录用户订单列表
    get_order_list(context) {
      getOrderList().then(response => {
        context.commit('set_order_list', response.data.data);
      });
    },
    //生成订单
    add_order(context) {
      // 订单信息
      let order = {
        //订购商品总金额
        order_spuamount: context.rootState.cart.total,
        //订单配送费
        order_expressfee: context.state.expressfee,
        //订单总金额
        order_totalamount: context.rootState.cart.total + context.state.expressfee,
        // 折扣金额
        order_discountamount: context.state.discountamount,
        // 实付金额
        order_payamount: context.rootState.cart.total + context.state.expressfee - context.state.discountamount
      }
      // 配送信息
      let addr = {
        // 收货人姓名
        'orderAddress.consignee': context.rootState.address.address.uaddr_name,
        //收货人电话
        'orderAddress.phone': context.rootState.address.address.uaddr_phone,
        //省
        'orderAddress.province': context.rootState.address.address.uaddr_province,
        //市
        'orderAddress.city': context.rootState.address.address.uaddr_city,
        //区
        'orderAddress.district': context.rootState.address.address.uaddr_district,
        //详细地址
        'orderAddress.address': context.rootState.address.address.uaddr_address,
      }
      //购物车中商品
      let orderDetailList = []
      for (let i = 0; i < context.rootState.cart.cart_list.length; i++) {
        let cart = context.rootState.cart.cart_list[i]
        // 判断 当前遍历到的购物车 是否 选中
        if (cart.cart_checked == 1) {
          let orderDetail = `{
          	"orderDetailList[${i}].odtails_name": "${cart.cart_name}",
          	"orderDetailList[${i}].odtails_thumburl" : "${cart.cart_thumburl}",
          	"orderDetailList[${i}].odtails_price" : "${cart.cart_price}",
          	"orderDetailList[${i}].odtails_count" : "${cart.cart_count}",
          	"orderDetailList[${i}].odtails_amount" : "${cart.cart_price * cart.cart_count}",
          	"orderDetailList[${i}].odtails_scorestatus" : "0",
          	"orderDetailList[${i}].odtails_spu_id" : "${cart.cart_spuid}",
          	"orderDetailList[${i}].odtails_sku" : ${JSON.stringify(cart.cart_sku)}
          }`
          orderDetailList.push(JSON.parse(orderDetail))
        }
      }
      //购物车中数据
      let order_submit = {}
      for (let index in orderDetailList) {
        for (let key in orderDetailList[index]) {
          order_submit[key] = orderDetailList[index][key];
        }
      }
      console.log({
        ...order,
        ...addr,
        ...order_submit
      })
      //合并 订单信息 配送信息 购物车中数据
      //提交订单
      /* addOrder({
        ...order,
        ...addr,
        ...order_submit
      }).then(response => {
        if (response.data.httpcode == 200) {
          // 订单添加成功的提示信息
          alert(`订单创建成功！订单号为：${response.data.data.order_no}`);
          // 将购物车列表中 已生成订单的项 删除
          // 循环购物车列表
          for (let i = 0; i <= context.rootState.cart.cart_list.length - 1; i++) {
            let cart = context.rootState.cart.cart_list[i]
            if (cart.cart_checked == 1) {
              context.dispatch('cart/delete_cart', {car_id: cart.car_id,index: i}, {root: true})
            }
          }
          // 路由定向到 OrderList 我的订单 页面
          router.push({
            path: '/customer/orderlist'
          })
        } else {
          alert(`订单创建失败！`);
        }
      }) */
    }
  }
}
